package com.core.tag.impl;

import java.io.IOException;
import java.util.Iterator;
import java.util.Map;

import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import org.apache.log4j.Logger;

import com.core.cache.Cache;
import com.core.util.DspUtil;
import com.dsp.bean.Company;

public class CompanyTag extends TagSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	Logger logger = Logger.getLogger(DspUtil.Log4J);

	private String htmlId;

	/**
	 * @return the htmlId
	 */
	public String getHtmlId() {
		return htmlId;
	}

	/**
	 * @param htmlId the htmlId to set
	 */
	public void setHtmlId(String htmlId) {
		this.htmlId = htmlId;
	}

	public int doStartTag() {
		JspWriter out = pageContext.getOut();
		StringBuffer select = new StringBuffer(100);
		select.append("<table cellpadding=\"4\" cellspacing=\"4\" border=\"0\" width='100%' id='allCompanys'>\n");
		Map<String, Company> map = Cache.getCompany();
		int i = 1;
		for (Iterator<Map.Entry<String, Company>> iter = map.entrySet().iterator(); iter.hasNext();) {
			Map.Entry<String, Company> entry = (Map.Entry<String, Company>) iter.next();
			String key = entry.getKey();
			Company value = entry.getValue();
			if(i == 1){
				select.append("<tr>");
			}
			select.append("<td width='25%' class='selecteds' ><input style='display:none;' type='checkbox' class='chk-company' checked name='"+ this.htmlId + "' value='"+key+"'>"+value.getCname()+"</td>");
			if(i % 4 == 0){
				select.append("</tr>");
				i = 1;
			} else{
				i++;
			}
		}
		if(i % 4 == 0){
			select.substring(0, select.length()-4);
		} else {
			for(int j = 0 ; j < i % 4 ; j++ )
				select.append("<td></td>");
			select.append("</tr>");
		}
		try {
			out.println(select);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return SKIP_BODY;
	}

}
